package com.cheyun.netsalev3.widget

import android.text.TextPaint

/**
 * @author Michael
 * @date  2018/12/24 - 10:20.
 */
object TextDrawUtils {
    /**
     * 获取文字的宽度
     */
    fun getTextWidth(text: String?, paint: TextPaint, size: Int): Float {
        if (text == null || text.isEmpty()) {
            return 0f
        }
        paint.textSize = size.toFloat()
        return paint.measureText(text)
    }

    /**
     * 获取文字的高度
     */
    fun getTextHeight(paint: TextPaint, size: Int): Float {
        paint.textSize = size.toFloat()
        val fontMetrics = paint.fontMetrics
        return fontMetrics.bottom - fontMetrics.top
    }

    /**
     * 获取文字的bottom
     */
    fun getTextBottom(paint: TextPaint, size: Int): Float {
        paint.textSize = size.toFloat()
        val fontMetrics = paint.fontMetrics
        return fontMetrics.bottom
    }

    /**
     * 给定文字的top获取文字的base line
     */
    fun getTextBaseLineByTop(top: Float, paint: TextPaint, size: Int): Float {
        paint.textSize = size.toFloat()
        val fontMetrics = paint.fontMetrics
        return top - fontMetrics.top
    }

    /**
     * 给定文字的bottom获取文字的base line
     */
    fun getTextBaseLineByBottom(bottom: Float, paint: TextPaint, size: Int): Float {
        paint.textSize = size.toFloat()
        val fontMetrics = paint.fontMetrics
        return bottom - fontMetrics.bottom
    }

    /**
     * 给定文字的center获取文字的base line
     */
    fun getTextBaseLineByCenter(center: Float, paint: TextPaint, size: Int): Float {
        paint.textSize = size.toFloat()
        val fontMetrics = paint.fontMetrics
        val height = fontMetrics.bottom - fontMetrics.top
        return center + height / 2 - fontMetrics.bottom
    }

    fun getTextBaseLineByCenter(center: Int, paint: TextPaint): Float {
        val fontMetrics = paint.fontMetrics
        val height = fontMetrics.bottom - fontMetrics.top
        return center + height / 2 - fontMetrics.bottom
    }
}
